package com.hypo.service.impl;

import com.hypo.annotation.LogOperation;
import com.hypo.mapper.DeptMapper;
import com.hypo.mapper.EmpMapper;
import com.hypo.pojo.Dept;
import com.hypo.until.MyException;
import com.hypo.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询所有
     *
     * @return 返回值
     */
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    /**
     * 根据Id删除
     *
     * @param id 参数
     * @return 返回值
     */
    @Override
    public Integer del(Integer id) {
        Integer num = empMapper.getId(id);
        if (num != null) {
            throw MyException.dept();
        } else {
            return deptMapper.del(id);
        }
    }

    /**
     * 添加
     */
    @Override
    public Integer add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.add(dept);
    }

    /**
     * 回显
     */
    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public Integer modify(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.modify(dept);
    }
}
